define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            $(".btn-edit").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/loss/index' + location.search,
                    add_url: 'stock/loss/add',
                    // edit_url: 'stock/loss/edit',
                    // del_url: 'stock/loss/del',
                    multi_url: 'stock/loss/multi',
                    table: 'stock_loss',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 4) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'lossid'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'type', title: __('Type'), searchList: {1:__('报损单'),2:__('报溢单')}},
                        {field: 'explain', title: __('Explain')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},

                        {field: 'posttime', title: __('Posttime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'status', title: __('状态'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=17")},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'printing',
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-print',
                                    hidden:function(row){

                                    },
                                    click: function(data){
                                        var lossid =$(this).parents('tr').find('.lossid').html()
                                        var url = Config.this_url+'/stock/loss/printing?ids='+lossid
                                        window.open(url)
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                if($(this).parents('tr').find('.btn-editone').length >0){
                    return  false;
                }
                var lossid = $(this).parents('tr').find('.lossid').html()
                var url = Config.this_url+'/stock/loss/detail?ids='+lossid
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "单据详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach?house_id='+$('#c-storehouse_id').val(),
                })
            })
            Controller.api.bindevent();
        },
        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/specs',
                    table: '',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'title', title: __('title'),  operate: false},
                        {field: 'place_origin', title: __('产地'), formatter: Table.api.formatter.search},
                        {field: 'number', title: __('编号')},
                        {field: 'unit', title: __('单位'), align: 'left'},
                        {field: 'specs', title: __('规格')},
                        {field: 'stock', title: __('库存')},

                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'posttime', title: __('过账时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);

            });

            // $(document).scroll(function() {
            //     if ($(document).height()-$(document).scrollTop()-window.innerHeight == 0) {
            //         console.log()
            //         if($('.pagination').find('.active').parents('div').css('display') == 'none'){
            //
            //         }else{
            //             setTimeout(function () {
            //                 $('.pagination').find('.active').next().click()
            //                 $('html,body').animate({scrollTop:0},1000);//回到顶端
            //             },500)
            //         }
            //
            //
            //         // console.log(1)
            //     } else {
            //         // console.log(2)
            //     }
            // })
            // $(window).scroll(function ()
            // {
            //     var st = $(this).scrollTop();
            // console.log(st)
            // if(st == 0){
            //     if($('.pagination').find('.active').parents('div').css('display') == 'none'){
            //
            //     }else{
            //         setTimeout(function () {
            //             $('.pagination').find('.active').prev().click()
            //             $('html,body').animate({scrollTop:0},1000);//回到顶端
            //         },500)
            //     }
            // }
            // });
            $('div.mousewheel_example')
                .bind('mousewheel', function(event, delta) {
                    var dir = delta > 0 ? 'Up' : 'Down',
                        vel = Math.abs(delta);
                    $(this).text(dir + ' at a velocity of ' + vel);
                    return false;
                });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/stock/loss/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});